﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;

namespace UI
{
    public enum ExplainSubPage
    {
        HerosSubPage = 0,
        WikiSubPage = 1,
        TutorialSubPage = 2
    }

    public class AutoChessExplainPage : OPPage
    {
        public Toggle[] tabs;
        public Transform subContentParent;

        private GameObject[] subContentObjs = new GameObject[3];
        private SubContentBase curSubContent;

        private void Start()
        {

        }

        protected override void DoOpen()
        {
            ExplainSubPage tabType = ExplainSubPage.HerosSubPage;

            if (Params != null)
            {
                tabType = (ExplainSubPage)Params;

            }

            OpenTab((int)tabType);
            tabs[(int)tabType].isOn = true;

            base.DoOpen();
        }

        protected override void DoClose(bool immediately)
        {
            //UnloadTabContentAsset();
            base.DoClose(immediately);
            foreach (var obj in subContentObjs)
            {
                if (obj != null)
                {
                    var subContent = obj.GetComponent<SubContentBase>();
                    if (subContent != null)
                        subContent.OnPageClose();
                }
            }
        }

        public void OpenTab(int tabIndex)
        {
            if (tabIndex < 0 || tabIndex >= tabs.Length)
            {
                ExLog.LogError("索引错误！");
                return;
            }

            if (curSubContent != null)
            {
                curSubContent.CloseTab();
            }

            if (subContentObjs[tabIndex] == null)
            {
                LoadTabContentAsset(tabIndex);
                curSubContent = subContentObjs[tabIndex].GetComponent<SubContentBase>();
                if (curSubContent != null)
                    curSubContent.ShowTab();
            }
            else
            {
                curSubContent = subContentObjs[tabIndex].GetComponent<SubContentBase>();
                if (curSubContent != null)
                    curSubContent.ReShowTab();
            }
        }

        private void LoadTabContentAsset(int tabIndex)
        {
            if (subContentObjs[tabIndex] != null)
            {
                return;
            }

            string path = "";
            switch ((ExplainSubPage)tabIndex)
            {
                default:
                case ExplainSubPage.HerosSubPage:
                    path = AssetLoadHelper.IN_BUNDLE_RES_PREFIX + "Pages/Chess_HerosSubPage";
                    break;
                case ExplainSubPage.TutorialSubPage:
                    path = AssetLoadHelper.IN_BUNDLE_RES_PREFIX + "Pages/Chess_TutorialSubPage";
                    break;
                case ExplainSubPage.WikiSubPage:
                    path = AssetLoadHelper.IN_BUNDLE_RES_PREFIX + "Pages/Chess_WikiSubPage";
                    break;
            }


            subContentObjs[tabIndex] = Instantiate(GetAssetBundleImmediately(path), subContentParent);
            subContentObjs[tabIndex].transform.localScale = Vector3.one;
            subContentObjs[tabIndex].transform.localPosition = Vector3.zero;
            subContentObjs[tabIndex].transform.localRotation = Quaternion.identity;
        }

    }
}
